<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="query">
				<el-form-item>
					<el-input v-model="query.keyword" placeholder="关键字"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="search">查询</el-button>
				</el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleAdd">新增</el-button>
        </el-form-item>
			</el-form>
		</el-col>

		<!--列表-->
		<el-table :data="pageInfo.rows" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
			<el-table-column type="selection" width="55">
			</el-table-column>
			<el-table-column type="index" width="60">
			</el-table-column>
			<el-table-column prop="key" label="键" sortable>
			</el-table-column>
      <el-table-column prop="value" label="值" sortable>
      </el-table-column>
      <el-table-column prop="intro" label="描述" sortable>
      </el-table-column>
      <el-table-column label="操作">
        <template scope="scope">
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
		</el-table>

		<!--工具条-->
		<el-col :span="24" class="toolbar">
      <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
			<el-pagination
          layout="prev, pager, next"
          @current-change="handleCurrentChange"
          :page-size="query.pageSize"
          :total="pageInfo.total" style="float:right;">
			</el-pagination>
		</el-col>

    <el-dialog title="新增/修改" :visible.sync="saveFormVisible" :close-on-click-modal="false">
      <el-form :model="saveForm" label-width="80px" :rules="saveFormRules" ref="saveForm">
        <!--prop="name" 引用saveFormRules规则中的 name规则-->
        <el-form-item label="键" prop="key">
          <el-input v-model="saveForm.key" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="值" prop="value">
          <el-input v-model="saveForm.value" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="描述">
          <el-input v-model="saveForm.intro" auto-complete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="saveFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="save" :loading="addLoading">提交</el-button>
      </div>
    </el-dialog>

	</section>
</template>

<script>

  export default {
		data() {
			return {
				listLoading: false,
				sels: [],//列表选中行

        saveFormRules: {
          name: [
            { required: true, message: '请输入名称', trigger: 'blur' }
          ],
          sn: [
            { required: true, message: '请输入标识', trigger: 'blur' }
          ]
        },

        // query对象是前端用来向后端传递参数的
        query:{
				  currentPage:1,
          pageSize:10,
          keyword:""
        },
        // pageInfo它是用来接收后端返回的数据的.用来渲染table表格和分页条的
        pageInfo:{
				  total:0,
          rows:[]
        },

        saveForm:{
				  id:null,
          key:"",
          value:"",
          intro:""
        },
        addLoading:false,    // 保存按钮,加载框
        saveFormVisible:false,   // 用来弹窗是否显示的
        saveLoading:false   // 设置权限那个加载框
			}
		},
		methods: {
      save: function () {
        // 校验表单中的输入是否满足规则
        this.$refs.saveForm.validate((valid) => {
          // valid==true,就说明表单是满足校验规则的
          if (valid) {
            this.$confirm('确认提交吗？', '提示', {}).then(() => {
              this.addLoading = true;
              this.$http.put("/config",this.saveForm)
                  .then(result => {
                      result = result.data;
                      // success message
                      // 关加载框
                      this.addLoading = false;

                      if(result.success){
                        this.$message({
                          message: "保存成功",
                          type: 'success'
                        });
                        // 刷新表格数据
                        this.getConfigs();

                        // 关弹框
                        this.saveFormVisible = false;
                      }else{
                        this.$message({
                          message: result.message,
                          type: 'error'
                        });
                      }
                  })
            });
          }
        });
      },
      handleAdd(){
          // 弹出 新增弹框
          this.saveFormVisible = true;
          // 赋初始值=空值,为了防止将上一次填写的内容带到下一次
          this.saveForm = {
            id:null,
            key:"",
            value:"",
            intro:""
          }
      },
      handleEdit(index,row){
          // 弹出 修改弹框
          this.saveFormVisible = true;
          // 回显  利用双向绑定原则做回显
          this.saveForm = row;
      },
      batchRemove: function () {
        this.$confirm('确认删除该记录吗?', '提示', {
          type: 'warning'
        }).then(() => {
          this.listLoading = true;  // 加载框
          // 获取要删除数据的id
          let ids = this.sels.map(x => x.id);

          this.$http.patch("/config",ids)
              .then(result => {
                result = result.data;
                // result {success:true,message:""}
                if(result.success){
                  // 删除成功
                  this.$message({
                    message: "删除成功",
                    type: 'success'
                  });
                  // 关闭加载框
                  this.listLoading = false;
                  // 跳转到第一页重新查询
                  this.search();
                }
              })
        }).catch(() => {

        });
      },
      selsChange(val){
          // val 就是当前被选中的所有的行的对象
          this.sels = val;
      },
      handleDel: function (index, row) {
        this.$confirm('确认删除该记录吗?', '提示', {
          type: 'warning'
        }).then(() => {
          this.listLoading = true;  // 加载框
          this.$http.delete("/config/"+row.id)
              .then(result => {
                  result = result.data;
                  // result {success:true,message:""}
                  if(result.success){
                    // 删除成功
                    this.$message({
                      message: "删除成功",
                      type: 'success'
                    });
                    // 关闭加载框
                    this.listLoading = false;
                    // 跳转到第一页重新查询
                    this.search();
                  }
              })
        }).catch(() => {

        });
      },
      search(){
        // 将当前页的值改为1
        this.query.currentPage = 1;
        this.getConfigs();
      },
			handleCurrentChange(val) {
				// val 当前要跳转的页码
        this.query.currentPage = val;
        this.getConfigs();
			},
			//获取权限列表
      getConfigs() {
				  this.$http.post("/config",this.query)
              .then(result => {
                  result = result.data;
                  console.log(result);
                  if(result.success){ // 判断后端操作是否成功
                    this.pageInfo = result.resultObj;
                  }else{
                    this.$message({
                      message: result.message,
                      type: 'error'
                    });
                  }
              })
              .catch(result => {
                this.$message({
                  message: '网络错误!',
                  type: 'error'
                });
              })
			},
		},
		mounted() {
		  // 页面一加载完毕,就要调用后端接口渲染列表
			this.getConfigs();
		}
	}

</script>

<style scoped>

</style>